var WebSocketURL = `ws://${import.meta.env.VITE_APP_SOCKET_SERVE.split('//')[1]}/fxfzcpxx/websocket`;

export function useWebsocket(){ //初始化weosocket
    //ws地址
    const ws = new WebSocket(WebSocketURL);
    
    const init = () =>{
        bindEvent();
    }
    function bindEvent(){
        ws.addEventListener('open',handleOpen,false)
        ws.addEventListener('close',handleClose,false)
        ws.addEventListener('error',handleError,false)
        ws.addEventListener('message',handleMessage,false)
    }

    function handleOpen (e) {
        console.log('websocket open', e);
    }
    function handleClose (e) {
        console.log('websocket close', e);
    }
    function handleError (e) {
        console.log('websocket error', e);
    }
    function handleMessage (e) {
        console.log('websocket message', e);
    }
    init();

    return ws;
}


